package com.example.snackfood.services.impl;

import com.example.snackfood.dao.SnackFoodProductsDao;
import com.example.snackfood.entity.Products;
import com.example.snackfood.services.IsnackFoodProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: snackFood
 * @description:
 * @author: lydms
 * @create: 2023-11-24 21:44
 **/

@Service
public class SnackFoodProductsService implements IsnackFoodProductsService {

    @Autowired
    private SnackFoodProductsDao snackFoodProductsDao;

    @Override
    public void save(Products products) {
        snackFoodProductsDao.save(products);
    }


    @Override
    public void delete(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        snackFoodProductsDao.delete(query);
    }

    @Override
    public void update(Products products) {
        Query query=new Query(Criteria.where("id").is(products.getId()));
        Update update=new Update();
        update.set("name",products.getName());
        update.set("description",products.getDescription());
        update.set("price",products.getPrice());
        update.set("category",products.getCategory());
        update.set("brand",products.getBrand());
        snackFoodProductsDao.update(query,update);
    }

    @Override
    public Products getProducts(Products products) {
        Criteria criteriaid1=Criteria.where("id").is(products.getId());
        Criteria criteriaid2=Criteria.where("name").is(products.getName());
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaid1,criteriaid2);
        Query query=new Query(orCriteria);
        return  snackFoodProductsDao.get(query);
    }

    @Override
    public List<Products> findAllProducts() {
        return snackFoodProductsDao.findAll();
    }
}
